<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29
 * Time: 13:41
 */

defined('BASEPATH') OR exit('No direct script access allowed');


class  Names extends CI_Controller
{
    /**
     * Names constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('ResponseUtil', null, 'resp');
        $this->load->model("namesmodel");
        $this->load->model("usermodel");
        $this->load->library('Common',null,'common');
    }

    /**
     * 记录查询日志
     */
    public function php_addQueryLog(){
        $params = json_decode($this->input->post('param'), true);
        $nameInfo = array(
            "nickname" => $params['nickname'],
            "lastname" => $params['lastname'],
            "namenum" => $params['double'],
            "sex" => $params['sex'],
            "mode" => $params['mode'],
            "openid" => $params['openid'],
            "unionid" => $params['unionid'],
            "birthday" => $params['birthdaydate'].' '.$params['birthdaytime'],
        );
        $this->namesmodel->addQueryLog($nameInfo);

    }

    /**
     * 分享成功后添加免费查看名字数量
     */
    public function php_pushCount(){
        $params = json_decode($this->input->post('param'), true);
        $num = $this->namesmodel->pushCount($params['openid']);
        $this->namesmodel->addShareLog($params['openid']);
        if($num>0){
            $this->resp->success(array('err_code' => 2,'count' => 50));
        }
    }

    /**
     * 获取名字组
     */
    public function php_getNamesGroup(){
        $url1 = 'https://qmsq.7dar.com/papilistname.php';
        //$url1 = 'http://localhost:81/qmsqcop/papilistname.php';
        $params = json_decode($this->input->post('param'), true);
        $lastname = $params['lastname'];
        $sex = $params['sex'];
        $double = $params['double'];
        $birthdaydate = $params['birthdaydate'];
        $birthdaytime = $params['birthdaytime'];
        $nickname = $params['nickname'];
        $openid = $params['openid'];
        $unionid = $params['unionid'];

        $mode = $params['mode'];
        $row = $this->usermodel->isTip($params['openid']);
        $tipCount = $row['num'];
        $this->namesmodel->updateUserRefreshCount($params['openid']);
        $jsonstr = $this->common->curl($url1.'?lastname='.urlencode($lastname).'&sex='.$sex.'&double='.$double.'&mode='.$mode.'&birthdaydate='.urlencode($birthdaydate).'&birthdaytime='.urlencode($birthdaytime).'&openid='.urlencode($openid).'&unionid='.urlencode($unionid).'&curpage=1&nickname='.urlencode($nickname).'&size=10');
        //echo $url1.'?lastname='.urlencode($lastname).'&sex='.$sex.'&double='.$double.'&mode='.$mode.'&birthdaydate='.urlencode($birthdaydate).'&birthdaytime='.urlencode($birthdaytime).'&openid='.urlencode($openid).'&unionid='.urlencode($unionid).'&curpage=1&nickname='.urlencode($nickname).'&size=10';
        $reqArr = json_decode($jsonstr, true);
        $nameList=$reqArr['namesGroup'];
        $query = $this->namesmodel->isSave($params['openid']);
        for($i = 0;$i<count($nameList);$i++){
            for($j = 0;$j<count($query);$j++){
                if($nameList[$i]['name']==$query[$j]['firstname'] && $query[$j]['lastname']==$params['lastname']){  //&&$query[$j]['lastname']==$lastnameInfo[0]['font']
                    $nameList[$i]['isfull']=1;
                    break;
                }else{
                    $nameList[$i]['isfull']=-1;
                }
            }
        }
        $this->resp->success(array('namelist'=> $nameList));
    }

    /**
     * 刷新名字列表
     */
    public function php_refreshNames(){

        $url1 = 'https://qmsq.7dar.com/papilistname.php';
        // = 'http://localhost:81/qmsqcop/papilistname.php';
        $params = json_decode($this->input->post('param'), true);
        $lastname = $params['lastname'];
        $sex = $params['sex'];
        $double = $params['double'];
        $birthdaydate = $params['birthdaydate'];
        $birthdaytime = $params['birthdaytime'];
        $nickname = $params['nickname'];
        $openid = $params['openid'];
        $unionid = $params['unionid'];
        $cur = $params['curpage'];
        $mode = $params['mode'];
        $row = $this->usermodel->isTip($params['openid']);
        $tipCount = $row['num'];



        $jsonstr = $this->common->curl($url1.'?lastname='.urlencode($lastname).'&sex='.$sex.'&double='.$double.'&mode='.$mode.'&birthdaydate='.urlencode($birthdaydate).'&birthdaytime='.urlencode($birthdaytime).'&openid='.urlencode($openid).'&unionid='.urlencode($unionid).'&curpage='.$cur.'&nickname='.urlencode($nickname).'&size=10');

        $reqArr = json_decode($jsonstr, true);
        $nameList=$reqArr['namesGroup'];
        if($params['flag']==true && count($nameList)>=10){
            $this->namesmodel->updateUserRefreshCount($params['openid']);
            $this->namesmodel->addUserRefreshLog($lastname,$openid);
        }
        $query = $this->namesmodel->isSave($params['openid']);
        for($i = 0;$i<count($nameList);$i++){
            for($j = 0;$j<count($query);$j++){
                if($nameList[$i]['name']==$query[$j]['firstname'] && $query[$j]['lastname']==$params['lastname']){  //&&$query[$j]['lastname']==$lastnameInfo[0]['font']
                    $nameList[$i]['isfull']=1;
                    break;
                }else{
                    $nameList[$i]['isfull']=-1;
                }
            }
        }
        $this->resp->success(array('namelist'=> $nameList));

    }


    public function php_getScore(){
        $url = "https://tqmsq.7dar.com/papinamescore.php";
        $params = json_decode($this->input->post('param'), true);
        $lastname = $params['lastname'];
        $firstname = $params['firstname'];
        $sex = $params['sex'];
        $birthdaydate =$params['birthdaydate'];
        $birthdaytime = $params['birthdaytime'];

        $jsonstr = $this->common->curl($url.'?lastname='.urlencode($lastname).'&sex='.$sex.'&birthdaydate='.urlencode($birthdaydate).'&firstname='.urlencode($firstname));

        $reqArr = json_decode($jsonstr, true);
        $this->resp->success($reqArr);
    }
    /**
     * 收藏名字
     */
    public function php_collectNames(){
        $params = json_decode($this->input->post('param'), true);
        $this->namesmodel->collectNames($params['savetype'],$params['openid'],$params['lastname'],$params['firstname'],$params['nameid'],$params['score'],$params['sex'],$params['birthday']);
        if($params['savetype']==1){
            $type=1;
        }else{
            $type=0;
        }
        $this->namesmodel->addCollectLog($params['openid'],$params['firstname'],$params['nameid'],$type);
    }

    /**
     * 获取收藏名字
     */
    public function php_getCollectNames(){
        $params = json_decode($this->input->post('param'), true);
        $collectList = $this->namesmodel->getCollectNames($params['openid']);
        $this->resp->success($collectList);
    }

    /**
     * 添加投票日志
     */
    public function php_addTicketLog(){
        $params = json_decode($this->input->post('param'), true);//openid,名字组，时间
        $nameList = $params['namelist'];
        $list=array();
        $j=0;
        for($i=0;$i<count($nameList);$i++){
            if($nameList[$i]['selected']==1){
                $list[$j]['openid']=$nameList[$i]['openid'];
                $list[$j]['name_id']=$nameList[$i]['name_id'];
                $list[$j]['lastname']=$nameList[$i]['lastname'];
                $list[$j]['firstname']=$nameList[$i]['firstname'];
                $list[$j]['score']=$nameList[$i]['score'];
                $list[$j]['selected']=$nameList[$i]['selected'];
                $j++;
            }
        }
        $names = json_encode($list);
        $tickid = $this->namesmodel->addTicketLog($params['openid'],$names);

        for($i = 0;$i<count($list);$i++){
            $this->namesmodel->addTicketNum($params['openid'],$list[$i]['lastname'],$list[$i]['firstname'],$tickid);
        }
        $this->resp->success(array('tickid'=>$tickid));
    }

    /**
     * 投票
     */
    public function php_updateTicketNum(){
        $params = json_decode($this->input->post('param'), true);
        $this->namesmodel->addRealTickLog($params['ticketid'],$params['lastname'],$params['firstname'],$params['selected'],$params['openid']);
        $this->namesmodel->updateTicketNum($params['ticketid'],$params['lastname'],$params['firstname'],$params['selected']);
    }

    /**
     * 获取投票记录
     */
    public function php_getTicketHistory(){
        date_default_timezone_set('PRC');
        $params = json_decode($this->input->post('param'), true);
        $historyList = $this->namesmodel->getTicketHistory($params['openid'],$params['page']);
        for($i = 0;$i<count($historyList);$i++){
            $timeStamp = $historyList[$i]['createtime'];
            $mon = date("m",strtotime($timeStamp));
            $day = date("d",strtotime($timeStamp));
            $historyList[$i]['createtime']=$mon.'月'.$day.'日';
        }
        $this->resp->success($historyList);

    }

    /**
     * 投票详情
     */
    public function php_getTicketDetail(){
        $params = json_decode($this->input->post('param'), true);
        $ticketList = $this->namesmodel->getTicketDetail($params['ticketid']);
        $realList = $this->namesmodel->getRealTickLog($params['ticketid'],$params['openid']);
        for($j=0;$j<count($ticketList);$j++){
            for($i=0;$i<count($realList);$i++){
                if($realList[$i]['lastname']==$ticketList[$j]['lastname'] && $realList[$i]['firstname']==$ticketList[$j]['firstname'])
                    $ticketList[$j]['selected']=1;
                continue;
            }
        }
        $openid = $ticketList[0]['fopenid'];
        $fuserinfo = $this->usermodel->queryUserByOpenId($openid);
        $babyname = $this->namesmodel->getUserN($params['ticketid']);
        $this->resp->success(array('fuserinfo'=>$fuserinfo,'ticketList'=>$ticketList,'babyname'=>$babyname,'list'=>$realList));
    }

    /**
     * 更新投票名
     */
    public function php_updateTicketLog(){
        $params = json_decode($this->input->post('param'), true);
        $this->namesmodel->updateTickLog($params['babyname'],$params['ticketid']);
    }

    /**
     * 添加分享记录
     */
    public function php_addShareLog(){
        $params = json_decode($this->input->post('param'), true);

        $this->namesmodel->addShareLog($params['openid']);
    }
    /**
     * 记录名字被测试的次数
     */
    public function php_addReviewCount(){
        $params = json_decode($this->input->post('param'), true);
        $this->namesmodel->addReviewCount($params['firstname']);
    }

    public function php_reviewName(){

    }
    public function php_getBazi($birthday){
        $getWuxingUrl = "https://qmsq.7dar.com/apigetxys?bazi=".urlencode(bazi);
    }
    public function test(){
        $url = "https://tqmsq.7dar.com/papinamescore.php";

        $list = $this->namesmodel->test();

        for($i=0;$i<count($list);$i++){
            $lastname = $list[$i]['lastname'];
            $firstname =$list[$i]['firstname'];
            $list[$i]['lastname']=$firstname;
            $list[$i]['firstname']=$lastname;
            $sex = 1;
            $birthdaydate ='2018-1-2';
            $jsonstr = $this->common->curl($url.'?lastname='.urlencode($lastname).'&sex='.$sex.'&birthdaydate='.urlencode($birthdaydate).'&firstname='.urlencode($firstname));
            $reqArr = json_decode($jsonstr, true);
            $list[$i]['score']=80;
       }
        $this->resp->success($list);
    }
}